import React, { Component } from 'react'
import './index.css'
import { nanoid } from 'nanoid'
import PropTypes from 'prop-types'
export default class Header extends Component {
  static propTypes = {
    addTodo: PropTypes.func.isRequired
  }

  handleKeyup = (e) => {
    const { keyCode, target } = e
    if (keyCode !== 13) return
    const value = target.value.trim()
    if (value) {
      const obj = {
        id: nanoid(),
        title: value,
        done: true
      }
      this.props.addTodo(obj)
    }
  }
  render() {
    return (
      <div className="todo-header">
        <input
          type="text"
          placeholder="请输入你的任务名称，按回车键确认"
          onKeyUp={this.handleKeyup}
        />
      </div>
    )
  }
}
